@echo off 
:: run  standard OpenHarmony Operationg System on one qemu machine
:: if you want two machines, please execute run2.bat

tasklist | findstr /i "qemu-system-x86_64.exe" 
if %errorlevel% equ 0 (
    echo "Please shutdown the running qemu virtual machines first"
    pause
    exit
) 

:: simulate a x86_64 machine
set cmdstr=qemu-system-x86_64

:: speed up the execution on windows
set cmdstr=%cmdstr% -accel whpx 

:: the simulated machine type
set cmdstr=%cmdstr% -machine q35 

::the cpu and memory specification
set cmdstr=%cmdstr% -smp 6 -m 4096M 

::boot from disk
set cmdstr=%cmdstr% -boot c 

::boot with no graphics
set cmdstr=%cmdstr% -nographic 

::with no vga card
set cmdstr=%cmdstr% -vga none 

::simulated a gpu card , with a fixed resolution  360X720
set cmdstr=%cmdstr% -device virtio-gpu-pci,xres=360,yres=720 

::simple display with no opengl
set cmdstr=%cmdstr% -display sdl,gl=off 

::sync the time with Windows
set cmdstr=%cmdstr% -rtc base=utc,clock=host 

::simulated a sound card
set cmdstr=%cmdstr% -device es1370 

:: the disk files store somewhere
:: fix it ， empty means the current directory
:: OHOS_IMG=   or  OHOS_IMG=D:\path1\   or OHOS_IMG=..\path2\   or  OHOS_IMG=path3\path4\
set OHOS_IMG=

:: machines can not share ramdisk, so copy it.
set ramdisk=%OHOS_IMG%ramdisk.img
set ramdiska=%OHOS_IMG%ramdiska.img
IF NOT EXIST %ramdiska% (
    echo copy %ramdisk% to %ramdiska% ...
    copy %ramdisk% %ramdiska%
    )

::machines should share bzImage
set cmdstr=%cmdstr% -kernel %OHOS_IMG%bzImage

::use qcow2 for reduce disk usage
IF NOT EXIST %OHOS_IMG%updater.qcow2 (
    echo convert updater.img to updater.qcow2 ...
    qemu-img convert -f raw -O qcow2 -c %OHOS_IMG%updater.img %OHOS_IMG%updater.qcow2       
    )
IF NOT EXIST %OHOS_IMG%system.qcow2 (
    echo convert system.img to system.qcow2 ...
    qemu-img convert -f raw -O qcow2 -c %OHOS_IMG%system.img %OHOS_IMG%system.qcow2
    )
IF NOT EXIST %OHOS_IMG%vendor.qcow2 (
    echo convert vendor.img to vendor.qcow2 ...
    qemu-img convert -f raw -O qcow2 -c %OHOS_IMG%vendor.img %OHOS_IMG%vendor.qcow2    
    )
IF NOT EXIST %OHOS_IMG%sys_prod.qcow2 (
    echo convert sys_prod.img to sys_prod.qcow2 ...
    qemu-img convert -f raw -O qcow2 -c %OHOS_IMG%sys_prod.img %OHOS_IMG%sys_prod.qcow2
    )
IF NOT EXIST %OHOS_IMG%chip_prod.qcow2 (
    echo convert chip_prod.img to chip_prod.qcow2 ...
    qemu-img convert -f raw -O qcow2 -c %OHOS_IMG%chip_prod.img %OHOS_IMG%chip_prod.qcow2
    )
IF NOT EXIST %OHOS_IMG%userdata.qcow2 (
    echo convert userdata.img to userdata.qcow2 ...
    qemu-img convert -f raw -O qcow2 -c %OHOS_IMG%userdata.img %OHOS_IMG%userdata.qcow2
    )

::machines can not share the same qcow2 file,  so create a overlay file for one machine.
IF NOT EXIST %OHOS_IMG%updatera.qcow2 (
    echo create updatera.qcow2 ...    
    qemu-img create -f qcow2 -b %OHOS_IMG%updater.qcow2 -F qcow2 %OHOS_IMG%updatera.qcow2 
    )
IF NOT EXIST %OHOS_IMG%systema.qcow2 (
    echo create systema.qcow2 ...   
    qemu-img create -f qcow2 -b %OHOS_IMG%system.qcow2  -F qcow2 %OHOS_IMG%systema.qcow2 
    )
IF NOT EXIST %OHOS_IMG%vendora.qcow2 (
    echo create vendora.qcow2 ...   
    qemu-img create -f qcow2 -b %OHOS_IMG%vendor.qcow2  -F qcow2 %OHOS_IMG%vendora.qcow2 
    )
IF NOT EXIST %OHOS_IMG%sys_proda.qcow2 (
    echo create sys_proda.qcow2 ...   
    qemu-img create -f qcow2 -b %OHOS_IMG%sys_prod.qcow2  -F qcow2 %OHOS_IMG%sys_proda.qcow2 
    )
IF NOT EXIST %OHOS_IMG%chip_proda.qcow2 (
    echo create chip_proda.qcow2 ...   
    qemu-img create -f qcow2 -b %OHOS_IMG%chip_prod.qcow2 -F qcow2 %OHOS_IMG%chip_proda.qcow2 
    )
IF NOT EXIST %OHOS_IMG%userdataa.qcow2 (
    echo create userdataa.qcow2 ...   
    qemu-img create -f qcow2 -b %OHOS_IMG%userdata.qcow2 -F qcow2 %OHOS_IMG%userdataa.qcow2 
    )

::the disk images
set drivestr=-drive if=virtio,media=disk,format=qcow2,index=0,file=%OHOS_IMG%updatera.qcow2 
set drivestr=%drivestr% -drive if=virtio,media=disk,format=qcow2,index=1,file=%OHOS_IMG%systema.qcow2, 
set drivestr=%drivestr% -drive if=virtio,media=disk,format=qcow2,index=2,file=%OHOS_IMG%vendora.qcow2, 
set drivestr=%drivestr% -drive if=virtio,media=disk,format=qcow2,index=3,file=%OHOS_IMG%sys_proda.qcow2, 
set drivestr=%drivestr% -drive if=virtio,media=disk,format=qcow2,index=4,file=%OHOS_IMG%chip_proda.qcow2 
set drivestr=%drivestr% -drive if=virtio,media=disk,format=qcow2,index=5,file=%OHOS_IMG%userdataa.qcow2 


::boot parameters for kernel
set bootstr=loglevel=4
set bootstr=%bootstr% console=ttyS0,115200 
set bootstr=%bootstr% init=init 
set bootstr=%bootstr% root=/dev/ram0 rw  
set bootstr=%bootstr% ohos.boot.hardware=virt 
set bootstr=%bootstr% default_boot_device=10007000.virtio_mmio 
set bootstr=%bootstr% ohos.required_mount.system=/dev/block/vdb@/usr@ext4@ro,barrier=1@wait,required 
set bootstr=%bootstr% ohos.required_mount.vendor=/dev/block/vdc@/vendor@ext4@ro,barrier=1@wait,required


::the device eth0, for access internet.  
::also can be connected by cmd "hdc tconn 127.0.0.1:55555" and controled by "hdc shell"
set netstr=-netdev user,id=net0,hostfwd=tcp::55555-:55555 -device e1000,netdev=net0


::serial number for the devices
set snstr=sn=88234567aa

set bootstr=%bootstr% %snstr%


set cmdstr=%cmdstr% -initrd %ramdiska% %drivestr% 
set cmdstr=%cmdstr% %netstr% -name OpenHarmony
set cmdstr=%cmdstr% -append 
set cmdstr=%cmdstr%^"%bootstr%^"

::start the machine 
start "OpenHarmony" cmd /k "%cmdstr%"  


